using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PS.WebParts.Rollup.GridViewWebPart
{
    public abstract class FormattedColumn
        : BoundField
    {
        protected string format;

        internal abstract string GetFormatedString(object container);

        internal virtual string GetDisplayString(object container)
        {
            return GetFormatedString(container);
        }

        protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            Literal input = new Literal();
            input.DataBinding += new EventHandler(input_DataBinding);
            cell.Controls.Add(input);
        }

        private void input_DataBinding(object sender, EventArgs e)
        {
            Literal control = (Literal)sender;
            Control controlContainer = control.NamingContainer;
            object component = DataBinder.GetDataItem(controlContainer);

            control.Text = GetFormatedString(component);
        }

        protected override bool SupportsHtmlEncode
        {
            get
            {
                return false;
            }
        }

        public string Format
        {
            get { return format; }
            set { format = value; }
        }
    }
}
